import { getTopCategoryAPI } from "@/apis/category";
import { onMounted, ref } from "vue";
import { useRoute, onBeforeRouteUpdate } from "vue-router";

export function useCategory() {
    const route = useRoute();
    const categoryData = ref({});
    const getCategory = async (id = route.params.id) => {
        const res = await getTopCategoryAPI(id);
        categoryData.value = res.result;
    };
    //解决路由缓存的问题，从小分类到小分类不跳转的问题，需要刷新才能跳转
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id);
    });
    onMounted(() => {
        getCategory();
    });
    return {
        categoryData
    }
}